package gui;

import java.awt.Component;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import javax.swing.JLayeredPane;

@SuppressWarnings("serial")
public class MyLayeredPane extends JLayeredPane implements MouseWheelListener {
	public MyLayeredPane() {
		addMouseWheelListener(this);
	}
	
	public MyJScrollPane addInLayer(int id) {
		MyJScrollPane scroll = new MyJScrollPane(id, new MyJTextPane());
		scroll.addMouseWheelListener(this);
		add(scroll, DEFAULT_LAYER);
		return scroll;
	}
	
	public MyJScrollPane getLayer(int id) {
		for (Component c : getComponents()) {
			if (c instanceof MyJScrollPane) {
				if (((MyJScrollPane)c).getID() == id) {
					return (MyJScrollPane)c;
				}
			}
		}
		
		return null;
	}
	
	public void removeLayer(int id) {
		MyJScrollPane toRemove = getLayer(id);
		if (toRemove != null) {
			remove(toRemove);
		}
	}
	
	public void mouseWheelMoved(MouseWheelEvent mwe) {
		int rotation = mwe.getWheelRotation();
		int top = getComponentCountInLayer(JLayeredPane.DEFAULT_LAYER);
		Component[] component = getComponentsInLayer(JLayeredPane.DEFAULT_LAYER);
		
		if (rotation > 0) {
			moveToBack(component[0]);
		} else {
			moveToFront(component[top - 1]);
		}
	}
}
